package com.example.learndevice.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import java.lang.Exception
import kotlin.concurrent.thread

/**
 *
 * @Author： LJH
 * @Time： 2022/7/5
 * @description：
 */
class TurnSurfaceView(context: Context,attributeSet: AttributeSet):SurfaceView(context,attributeSet),SurfaceHolder.Callback {
    private lateinit var mPaint1:Paint
    private lateinit var mPaint2:Paint
    private lateinit var mRectF:RectF
    private var mBeginAngle1 = 0
    private var mBeginAngle2 = 180
    private var mInterval = 70
    private var bRunning = false

    private lateinit var mHolder: SurfaceHolder
    private lateinit var mCanvas: Canvas

    init {
        //getHolder方法获得SurfaceHolder对象，然后进行预览界面的相关绘图操作。
        mHolder = holder
        mHolder.addCallback(this)
        //下面两行设置背景为透明，因为SurfaceView默认背景是黑色
        setZOrderOnTop(true)
        mHolder.setFormat(PixelFormat.TRANSLUCENT)
    }

    @SuppressLint("DrawAllocation")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //直径
        val diameter = measuredWidth - paddingLeft - paddingRight
        mRectF = RectF(paddingLeft.toFloat(),paddingTop.toFloat(),(paddingLeft+diameter).toFloat(),(paddingTop+diameter).toFloat())
    }

    private fun draw(paint: Paint,beginAngle:Int){
        synchronized(mHolder){
            //锁定并获取绘图表面的画布。
            mCanvas = mHolder.lockCanvas()
            // SurfaceView上次的绘图结果仍然保留，如果不想保留上次的绘图，则需将整个画布清空
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawArc(mRectF,beginAngle.toFloat(),10F,true,paint)
            //解锁并刷新绘图表面的画布。
            mHolder.unlockCanvasAndPost(mCanvas)
        }
    }

    fun start(){
        bRunning = true
        thread {
            while (bRunning){
                draw(mPaint1,mBeginAngle1)
                try {
                    Thread.sleep(mInterval.toLong())
                }catch (e:Exception){
                    e.printStackTrace()
                }
                mBeginAngle1 += 3
            }
        }
        thread {
            while (bRunning){
                draw(mPaint2,mBeginAngle2)
                try {
                    Thread.sleep(mInterval.toLong())
                }catch (e:Exception){
                    e.printStackTrace()
                }
                mBeginAngle2 += 3
            }
        }
    }

    fun stop(){
        bRunning = false
    }

    private fun getPaint(color:Int):Paint{
        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = color
        paint.strokeWidth = 10f
        paint.style = Paint.Style.FILL
        return paint
    }

    /**
     * 在绘图表面创建后触发，可在此打开相机。
     * @param holder SurfaceHolder
     */
    override fun surfaceCreated(holder: SurfaceHolder) {
        mPaint1 = getPaint(Color.RED)
        mPaint2 = getPaint(Color.CYAN)
    }

    /**
     * 在绘图表面变更后触发。
     * @param holder SurfaceHolder
     * @param format Int
     * @param width Int
     * @param height Int
     */
    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    /**
     * 在绘图表面销毁后触发。
     * @param holder SurfaceHolder
     */
    override fun surfaceDestroyed(holder: SurfaceHolder) {

    }

}